﻿Ext.ns('suce.clasificacion');

suce.clasificacion.search = function (config) {

    return new Ext.Dialog(Ext.apply({

        width: 700,
        height: 400,
        title: 'Clasificación',
        layout: 'border',

        initComponent: function () {

            var me = this,
                form;

            me.addEvents('select');

            form = new Ext.FormPanel({
                region: 'north',
                baseCls: 'x-plain',
                height: 50,
                bodyPadding: 10,
                fieldDefaults: { labelWidth: 90, anchor: '100%' },
                items: [
                    TEXT('nombclasificacion', 'Nombre', 200)
                ],
                keys: { key: 13, handler: me.reload, scope: me }
            });

            me.grid = new Ext.grid.Panel({
                region: 'center',
                store: STORE('mant.ashx', { C: 'CLASIFICACION', S: 'SEARCH' }, 'idclasificacion,codiclas,nombclasificacion,codigrupo'),
                columns: COLMODEL([
                    { xtype: 'rownumberer', width: 23 },
                    ['codiclas', 'Código', 100],
                    ['nombclasificacion', 'Clasificación', 0.1],
                    ['codigrupo', 'Grupo', 70]
                ], { sortable: true }),
                listeners: {
                    scope: me,
                    'itemdblclick': me.select
                },
                tbar: ['->', { text: '<b>Buscar</b>', handler: me.reload, scope: me, iconCls: 'tb-search'}],
                bbar: [
                    '->',
                    { text: '<b>Seleccionar</b>', handler: me.select, scope: me, iconCls: 'tb-insert' },
                    { text: 'Cerrar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
                ]
            });

            me.tools = [{ type: 'refresh', handler: me.reload, scope: me}];
            me.items = [form, me.grid];
            Ext.Dialog.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function () {
            var me = this;
            Ext.Dialog.prototype.show.call(me);
        },

        reload: function () {
            var me = this;
            me.grid.store.load({ params: me.form.getValues() });
        },

        select: function () {
            var me = this,
                rs = GETSELECTED(me.grid, true);
            if (!rs) return;
            if (me.fireEvent('select', rs) !== false)
                me.onEsc();
        }

    }, config));
};